package cappuccino.couldbook.ui;

import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cappuccino.couldbook.Constants;
import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.data.User;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public class CreateGroupActivity extends BaseActivity implements View.OnClickListener {

    String TAG = "LoginActivity";

    @Override
    protected void init() {

    }

    TextInputLayout tilName, tilDetaiil;
    Button btSubmit,btnRegister;

    @Override
    protected void bindView() {
        setContentView(R.layout.activity_creategroup);
        tilName = (TextInputLayout) findViewById(R.id.til_create_groupname);
        tilDetaiil = (TextInputLayout) findViewById(R.id.til_create_groupdetail);
        btSubmit = (Button) findViewById(R.id.btn_createsubmit);
        btSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_createsubmit:
                create();
                break;

            default:
                break;
        }
    }

    Observer<Result<Group>> observer = new Observer<Result<Group>>() {

        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.v(TAG, "onError");
            Toast.makeText(CreateGroupActivity.this.getApplicationContext(), "We meet some problem ", Toast.LENGTH_SHORT).show();
            Log.v(TAG, throwable.toString());
        }

        @Override
        public void onNext(Result<Group> res) {
            Log.v(TAG, "OnNext");
            if (res.status == 1) {
                Toast.makeText(CreateGroupActivity.this.getApplicationContext(), "Create " + res.data.name + " success", Toast.LENGTH_SHORT).show();
                    finish();
            } else
                Toast.makeText(CreateGroupActivity.this.getApplicationContext(), "Username and password wrong", Toast.LENGTH_SHORT).show();
        }
    };

    private void create(String name,String detail){
        NetApi api = Network.getNetApi();
        AccountM accountM = new AccountM(this);
        int userId = accountM.getAccout();
        if(userId != -1)
           api.createGroup(userId,name,detail).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    private void create() {
        Log.v(TAG, "clicked");
        String name = tilName.getEditText().getText().toString();
        String detail = tilDetaiil.getEditText().getText().toString();
        boolean info = true;
        if (name == null || name.isEmpty()) {
            tilName.setError("社团名不能为空");
            info = false;
        }
        if (detail == null || detail.isEmpty()) {
            tilDetaiil.setError("简介不能为空");
            info = false;
        }
        if (info) {
            create(name,detail);
        }
    }
}
